#include "stm32f10x.h"                  // Device header

#define BlueTooth_Clock							RCC_APB2Periph_GPIOA
#define BlueTooth_Usart_Clock				RCC_APB1Periph_USART2
#define BlueTooth_Tx_Port						GPIOA
#define BlueTooth_Tx_Pin						GPIO_Pin_2
#define BlueTooth_Rx_Port						GPIOA
#define BlueTooth_Rx_Pin						GPIO_Pin_3
#define BlueTooth_Usart							USART2

void BlueTooth_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(BlueTooth_Clock | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(BlueTooth_Usart_Clock, ENABLE);
	
	/* 配置USART2 Tx (PA.2)为复用推挽输出 */
	GPIO_InitStructure.GPIO_Pin = BlueTooth_Tx_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(BlueTooth_Tx_Port, &GPIO_InitStructure);
	
	/* 配置USART2 Rx (PA.3)为浮空输入 */
	GPIO_InitStructure.GPIO_Pin = BlueTooth_Rx_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(BlueTooth_Rx_Port, &GPIO_InitStructure);

	/* USART2配置 */
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(BlueTooth_Usart, &USART_InitStructure);
	
	/* 配置USART2中断 */
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* 使能USART2接收中断 */
	USART_ITConfig(BlueTooth_Usart, USART_IT_RXNE, ENABLE);
	
	/* 使能USART2 */
	USART_Cmd(BlueTooth_Usart, ENABLE);
}
